package com.wondream.myframework.heima_concurrent;


import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.*;

/**
 * 第四种创建线程的方式:使用线程池-ThreadPoolExecutor
 */
public class _006_Thread_ThreadPoolExecutor {

    public static void main(String[] args) throws Exception {
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("thread-pool-%d").build();
//        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        ExecutorService executorService = new ThreadPoolExecutor(
                5, //线程池初始线程数
                5, //线程池最大线程数
                10, //线程空闲存活时间 时间到了，线程会被终止；当且仅当线程池中的线程数大于corePoolSize时才会起作用
                TimeUnit.SECONDS, //存活时间单位
                new ArrayBlockingQueue<>(6), //阻塞队列workQueue
                threadFactory, //创建线程的工厂
                new ThreadPoolExecutor.AbortPolicy() //放弃策略
        );

        for (int i = 0; i < 5; i++) {
            executorService.execute(() -> {
                System.out.println("执行了run方法");
            });
        }

        System.out.println("第四种创建线程的方式:使用线程池-ThreadPoolExecutor");;
    }
}

